package cn.xinjiu.reggie.service.impl;

import cn.xinjiu.reggie.common.CustomException;
import cn.xinjiu.reggie.dto.SetmealDto;
import cn.xinjiu.reggie.entity.Dish;
import cn.xinjiu.reggie.entity.Setmeal;
import cn.xinjiu.reggie.entity.SetmealDish;
import cn.xinjiu.reggie.mapper.SetmealMapper;
import cn.xinjiu.reggie.service.SetmealDishService;
import cn.xinjiu.reggie.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by xinchen on 2022/7/4
 *
 * @Description : 套餐服务实现类
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * @param setmealDto
     * @return
     * @description TODO 新增套餐，同时保存套餐和菜品关系
     */
    @Override
    @Transactional // 保证事务的一致性
    public void saveWithDish(SetmealDto setmealDto) {
        // 1. 保存套餐
        this.save(setmealDto);
        // 2. 保存套餐和菜品的关系
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * @param ids
     * @return
     * @description TODO 删除套餐，同时删除套餐和菜品的关联数据
     */
    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        /**
         *   select count(*) from setmeal where id in (1,2,3) and status = 1
         *   如果该查询的值 > 0 ，说明套餐在售，不可删除
         */
        // 先查询套餐状态，是否可以删除
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId, ids);
        queryWrapper.eq(Setmeal::getStatus, 1);
        long count = this.count(queryWrapper);
        if (count > 0) {
            // 不能删除，抛出自定义用户异常
            throw new CustomException("套餐正在售卖中，不能删除");
        }

        // 删除套餐
        this.removeByIds(ids);

        // 删除套餐对应的菜品
        /**
         *   delete from setmeal_dish where setmeal_id in (1,2,3)
         */
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(lambdaQueryWrapper);

    }

    /**
     * 根据id查套餐信息及对应的菜品信息
     * @param id
     * @return
     */
    @Override
    public SetmealDto getByIdWithFlavor(Long id) {
        Setmeal setmeal = this.getById(id);

        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);

        LambdaQueryWrapper<SetmealDish> eq = new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId, setmeal.getId());
        // 菜品信息集合
        List<SetmealDish> list = setmealDishService.list(eq);
        setmealDto.setSetmealDishes(list);

        return setmealDto;
    }

}
